<?php 
include('../../Connections/KRAPPERCON.php');
?>

<?php 

//if (!getVal('codcli')!='')
$tipo = $_POST["tipo"];
$tipo = trim(getVal('codcli'));
$oper = $_POST['oper'];
$callback = getVal('callback');


if ($oper == "save"){
$cod = getVal('codcli');
$nom = getVal('nombrecli');
$apat = getVal('apepatcli');
$amat = getVal('apematcli');
$dir1 = getVal('direccioncli');
$est = getVal('est');
$doccli = getVal('docucli');
$ubi = getVal('cb_ubigeo');
$dir2 = getVal('direccion2cli');
$cel = getVal('celularcli');
$tel = getVal('tlfcli');
$cor = getVal('correocli');
$cont = getVal('contactocli');
$tipo = getVal('tipocli');
$zona = getVal('zona');

$pais = getVal('pais');
$reg1 = getVal('reg1');

$query = "call sp_saveKUN('".$cod."','".$nom."','".$apat."','".$amat."','".$dir1."','".$est."','".$doccli."','".$ubi."','".$dir2."','".$cel."','".$tel."','".$cor."','".$cont."','".$tipo."','".$zona."','".$user_now."',' ','".$pais."','".$reg1."');";

doEcho ($query);
$res = $mysqli->query($query);
if ($res) {
		if($row = $res->fetch_assoc()) {
			if ($row['info']>0) {
			$ret = getVal('finder');
			//$scp = "loadPage('Mantenimientos/MantCli.php');";
			if (trim($ret)!=''){
				$scp = $ret . '("' . $row['info'] . '");closePopup();';
			}
			echo "<script type='text/javascript'>"."showOk('Datos grabados correctamente para el Cliente : ".$row['info']."');";
			if (trim($callback)!=""){
			echo $callback."({ cod :'".$row['info']."',nif:'".$doccli."', nom:'".$nom."', ape:'".$apat." ".$amat."',dir:'".$dir1."',dir2:'".$dir2."'});";
			}
			echo "</script>"; 
			}
			else {
			echo "<script type='text/javascript'>showError('".$row['msg_err']."'); </script>"; 
			}
		}
$res->close();
$mysqli->check_result();
}


}else{

$query = "
call sp_actucliente('".$_POST['codcli']."','".$_POST['nombrecli']."','".$_POST['apepatcli']."','".$_POST['apematcli']."','".$_POST['direccioncli']."',
'".$_POST['est']."','".$_POST['docucli']."','".$_POST['cb_ubigeo']."','".$_POST['direccion2cli']."',
'".$_POST['celularcli']."','".$_POST['tlfcli']."',
'".$_POST['correocli']."','".$_POST['contactocli']."',".$_POST['tipocli'].",'".$_POST['zona']."', ' ');
"  or die($mysqli->error.__LINE__);
doEcho ($query);
$res = $mysqli->query($query);
if ($res) {
		if($row = $res->fetch_assoc()) {
			if ($row['info']>0) {
			echo "<script>showOk('Se ha actualizado el Cliente : ".$row['info']."');loadPage('Mantenimientos/MantCli.php'); </script>"; 
			}
			else {
			echo "<script>showError('".$row['msg_err']."'); </script>"; 
			}
		}
$res->close();
$mysqli->next_result();
}

}


/* echo "<script>loadPage('Mantenimientos/MantCli.php'); </script>";
echo "<script>showOk('OPERACION EXISTOSA!!!'); </script>"; 
*/
?>